using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThridGroup.ERP.Application.Contracts.DTO.WarehouseManagement;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Areas.WarehouseManagement.Controllers
{
    [Area("WarehouseManagement")]
    [Route("WarehouseManagement/[controller]")]
    public class WarehouseAreaSettingController : Controller
    {
        private readonly IWarehouseAreaSettingService _warehouseAreaSettingService;
        private readonly IWarehouseSettingService _warehouseSettingService;

        public WarehouseAreaSettingController(
            IWarehouseAreaSettingService warehouseAreaSettingService,
            IWarehouseSettingService warehouseSettingService)
        {
            _warehouseAreaSettingService = warehouseAreaSettingService;
            _warehouseSettingService = warehouseSettingService;
        }

        /// <summary>
        /// 库区设置列表页面
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("")]
        [Route("Index")]
        public IActionResult Index(int warehouseId)
        {
            ViewBag.WarehouseId = warehouseId;
            return View();
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id">库区ID，0表示新增</param>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Edit")]
        public async Task<IActionResult> Edit(int id, int warehouseId)
        {
            if (id > 0)
            {
                // 编辑现有库区
                var model = await _warehouseAreaSettingService.GetByIdAsync(id);
                if (model != null)
                {
                    ViewBag.Model = model;
                }
            }
            else
            {
                // 新增库区
                ViewBag.WarehouseId = warehouseId;
            }

            return View();
        }

        /// <summary>
        /// 获取库区设置列表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetWarehouseAreaSettings")]
        public async Task<IActionResult> GetWarehouseAreaSettings([FromQuery] GetWarehouseAreaSettingListInput input)
        {
            var result = await _warehouseAreaSettingService.GetListAsync(input);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = result.Total,
                data = result.Items
            });
        }

        /// <summary>
        /// 获取库区设置详情
        /// </summary>
        /// <param name="id">库区ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Get/{id}")]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _warehouseAreaSettingService.GetByIdAsync(id);

            return Json(new
            {
                code = 0,
                msg = "成功",
                data = result
            });
        }

        /// <summary>
        /// 创建库区设置
        /// </summary>
        /// <param name="input">创建参数</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Create")]
        public async Task<IActionResult> Create([FromBody] CreateUpdateWarehouseAreaSettingDto input)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { code = 1, msg = "参数错误" });
            }

            try
            {
                var result = await _warehouseAreaSettingService.CreateAsync(input);
                return Json(new { code = 0, msg = "添加成功", data = result });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "添加失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 更新库区设置
        /// </summary>
        /// <param name="id">库区ID</param>
        /// <param name="input">更新参数</param>
        /// <returns></returns>
        [HttpPut]
        [Route("Update/{id}")]
        public async Task<IActionResult> Update(int id, [FromBody] CreateUpdateWarehouseAreaSettingDto input)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { code = 1, msg = "参数错误" });
            }

            try
            {
                var result = await _warehouseAreaSettingService.UpdateAsync(id, input);
                return Json(new { code = 0, msg = "更新成功", data = result });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "更新失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 删除库区设置
        /// </summary>
        /// <param name="id">库区ID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Delete/{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                await _warehouseAreaSettingService.DeleteAsync(id);
                return Json(new { code = 0, msg = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 检查库区编码是否已存在
        /// </summary>
        [HttpGet]
        [Route("CheckCodeExists")]
        public async Task<JsonResult> CheckCodeExists(string code, int id = 0)
        {
            var exists = await _warehouseAreaSettingService.IsCodeExistAsync(code, id);
            return Json(new { exists });
        }
    }
} 